﻿namespace InkType.ViewModel

open System
open System.Collections.Generic
open System.Collections.ObjectModel
open System.Collections.Specialized
open System.ComponentModel
open System.Windows
open System.Windows.Data


type RecentItemsQueue<'a> (capacity : int) =
    let buffer = ObservableCollection<'a> ()

    member x.Push item =
        buffer.Insert (0, item)
        if buffer.Count > capacity then
            buffer.RemoveAt (buffer.Count - 1)

    member x.Clear () =
        buffer.Clear ()

    member x.Count = buffer.Count

    member x.Item
        with get (i : int) = buffer.[i]

    interface System.Collections.IEnumerable with
        member x.GetEnumerator () =
            (buffer :> System.Collections.IEnumerable).GetEnumerator ()

    interface IEnumerable<'a> with
        member x.GetEnumerator () =
            buffer.GetEnumerator ()

    interface IReadOnlyCollection<'a> with
        member x.Count = x.Count

    interface IReadOnlyList<'a> with
        member x.Item
            with get (i : int) = x.[i]

    interface INotifyCollectionChanged with
        [<CLIEvent>] member x.CollectionChanged = buffer.CollectionChanged

    interface INotifyPropertyChanged with
        [<CLIEvent>] member x.PropertyChanged = (buffer :> INotifyPropertyChanged).PropertyChanged

